package com.yifushidai.config;

import com.yifushidai.entity.*;
import com.yifushidai.mapper.ApiUniqueMacMapper;
import com.yifushidai.mapper.OptDeviceLogMapper;
import com.yifushidai.service.DynamicTempkeyService;
import com.yifushidai.service.IcBindService;
import com.yifushidai.service.SfzUploadService;
import com.yifushidai.utils.ShisuMinaPushUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class RedisMessageReceiver {
    private Logger logger = LoggerFactory.getLogger(RedisMessageReceiver.class);

    @Autowired
    private IcBindService icBindService;
    @Autowired
    private DynamicTempkeyService dynamicTempkeyService;
    @Autowired
    private SfzUploadService sfzUploadService;

    @Autowired
    private OptDeviceLogMapper optDeviceLogMapper;
    @Autowired
    private ApiUniqueMacMapper apiUniqueMacMapper;

    /**
     * 接收redis消息，并处理
     *
     * @param message 过期的redis key
     */
    public void receiveMessage(String message) throws InterruptedException {
        logger.warn("通知的key是：{}", message);

        if (message.matches("^IC_\\w{24}_[012]$")) {
            String[] arr = message.split("_");
            IcBind record = new IcBind();
            record.setMac(arr[1]);
            record.setIcType(arr[2]);

            icBindService.delete(record);

            StringBuilder param = new StringBuilder();
            param.append("超时自动解绑C").append(record.getIcType()).append("卡 ");
            ApiUniqueMac one = apiUniqueMacMapper.selectOne(ApiUniqueMac.builder().mac(record.getMac()).build());
            Object res = ShisuMinaPushUtils.push(record.getMac(), "icUnbind:" + record.getIcType());
            if ("OK".equals(res)) {
                optDeviceLogMapper.insertSelective(OptDeviceLog.builder().addressSocket("redis").mobile(one.getMobile()).uniqueMac(one.getMac()).alias(one.getAddr() + one.getAlias()).optDesc(param.append("任务成功").toString()).build());
            } else {
                optDeviceLogMapper.insertSelective(OptDeviceLog.builder().addressSocket("redis").mobile(one.getMobile()).uniqueMac(one.getMac()).alias(one.getAddr() + one.getAlias()).optDesc(param.append("任务失败").toString()).build());
            }

        }
        else if (message.matches("^TK_\\w{24}_\\d+$")) {
            String[] arr = message.split("_");
            DynamicTempkey record = new DynamicTempkey();
            record.setMac(arr[1]);
            record.setPushIndex(Integer.parseInt(arr[2]));

            dynamicTempkeyService.delete(record);

            StringBuilder param = new StringBuilder();
            param.append("超时自动删除索引").append(Integer.parseInt(arr[2])).append("密码 ");
            ApiUniqueMac one = apiUniqueMacMapper.selectOne(ApiUniqueMac.builder().mac(record.getMac()).build());
            optDeviceLogMapper.insertSelective(OptDeviceLog.builder().addressSocket("redis").mobile(one.getMobile()).uniqueMac(one.getMac()).alias(one.getAddr() + one.getAlias()).optDesc(param.append("任务成功").toString()).build());

        }
//        else if (message.matches("^SFZ_\\w{24}_\\d{17}[\\dXx]$")) {
//            String[] arr = message.split("_");
//            SfzUpload record = new SfzUpload();
//            record.setMac(arr[1]);
//            record.setGmsfzhm(arr[2]);
//
//            sfzUploadService.delete(record);
//
//            StringBuilder param = new StringBuilder();
//            param.append("超时自动删除身份证").append(arr[2]).append(" ");
//            ApiUniqueMac one = apiUniqueMacMapper.selectOne(ApiUniqueMac.builder().mac(record.getMac()).build());
//            optDeviceLogMapper.insertSelective(OptDeviceLog.builder().addressSocket("redis").mobile(one.getMobile()).uniqueMac(one.getMac()).alias(one.getAddr() + one.getAlias()).optDesc(param.append("任务成功").toString()).build());
//
//        }
    }
}
